import 'package:flutter/material.dart';
import 'package:logger/logger.dart';

class LifecyclePage extends StatefulWidget {
  const LifecyclePage({super.key});

  @override
  State<LifecyclePage> createState() => _LifecyclePageState();
}

class _LifecyclePageState extends State<LifecyclePage> {
  /// 初始化  常用
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    Logger().i('initState');
  }

  /// 第一构建时  更新时调用
  @override
  void didChangeDependencies() {
    // TODO: implement didChangeDependencies
    super.didChangeDependencies();
    Logger().i('didChangeDependencies');
  }

  /// 父组件需要重绘制时才调用
  @override
  void didUpdateWidget(covariant LifecyclePage oldWidget) {
    // TODO: implement didUpdateWidget
    super.didUpdateWidget(oldWidget);
    Logger().i('didUpdateWidget');
  }

  var count = 0;

  ///   setState 会调用   常用
  @override
  Widget build(BuildContext context) {
    Logger().i('build');
    return Scaffold(
      appBar: AppBar(
        title: Text('Flutter生命周期'),
      ),
      body: Center(
        child: Column(
          children: [
            TextButton(
                onPressed: () {
                  setState(() {
                    count++;
                  });
                },
                child: Text(
                  '加+  build被调用一次',
                  style: TextStyle(fontSize: 26),
                )),
            TextButton(onPressed: () {}, child: Text('$count')),
          ],
        ),
      ),
    );
  }

  @override
  void deactivate() {
    // TODO: implement deactivate
    super.deactivate();
    Logger().i('deactivate');
  }

  /// 销毁时 最后调用  常用
  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    Logger().i('dispose');
  }
}
